package backend;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeDateValidator {
	
	/**
	 * This method reads the Time in String and returns in Integer.
	 * 
	 * @param time  "@1700" for 5pm in String format
	 * @return IntTime  1700 for 5pm in Integer format
	 */
	public int StartTime(String time) {
		int IntTime=0;
		int k=4;
		char[] charTime;
		
		charTime = time.toCharArray();
		for(int i=1; i<=1000; i*=10) {
			IntTime = IntTime + (charTime[k--]-48)*i;
		}
		return IntTime;
	}

	/**
	 * This method read in both the Start and Due time of a Task but only
	 * returns the Due time in Integer format.
	 * 
	 * @param time  "@1700-1800" for 5-6pm in String format
	 * @return IntTime  1800 for 6pm in Integer format
	 */
	public int DueTime(String time) {
		int IntTime=0;
		int k=9;
		char[] charTime;
		
		charTime = time.toCharArray();
		for(int i=1; i<=1000; i*=10) {
			IntTime = IntTime + (charTime[k--]-48)*i;
		}
		return IntTime;
	}

	/**
	 * This method reads the Date in String format and returns the Year, YY, 
	 * in Integer format.
	 * 
	 * @param date  "300512" in ddMMyy for 30th May 2012 in String format
	 * @return int  12 for year 2012 in Integer format
	 */
	public int ConvertDateYY(String date) {
		int yy;
		char[] ddmmyy;
		
		ddmmyy = date.toCharArray();
		yy = ((ddmmyy[4]-48)*10) + (ddmmyy[5]-48);

		return yy;
	}
	
	/**
	 * This method reads the Date in String format and returns the Month, MM, 
	 * in Integer format.
	 * 
	 * @param date  "300512" in ddMMyy for 30th May 2012 in String format
	 * @return int  5 for month May in Integer format
	 */
	public int ConvertDateMM(String date) {
		int mm;
		char[] ddmmyy;
		
		ddmmyy = date.toCharArray();
		mm = ((ddmmyy[2]-48)*10) + (ddmmyy[3]-48);
		
		return mm;
	}
	
	/**
	 * This method reads the Date in String format and returns the Day, DD, 
	 * in Integer format.
	 * 
	 * @param date  "300512" in ddMMyy for 30th May 2012 in String format
	 * @return int  30 for Day 30 in Integer format
	 */
	public int ConvertDateDD(String date) {
		int dd;
		char[] ddmmyy;
		
		ddmmyy = date.toCharArray();
		dd = ((ddmmyy[0]-48)*10) + (ddmmyy[1]-48);

		return dd;
	}

	/**
	 * This method reads the Start Time or Start-Due Time in String format
	 * but only returns the Start Time.
	 * 
	 * @param temp  The Start Time or Start-Due Time in String format: ie, @1700 or @1700-1800
	 * @return  The Start time in String format: ie, "1700"
	 */
	public String GetStartTime(char[] temp) {
		int k=1;
		String tempWord="";
		while(k<5) {
			tempWord = tempWord + temp[k];
			k++;
		}
		return tempWord;
	}
	
	/**
	 * This method reads Start-Due Time in String format
	 * but only returns the Due Time.
	 * 
	 * @param temp  The Start-Due Time in String format: ie, @1700-1800
	 * @return  The Due time in String format: ie, "1800"
	 */
	public String GetDueTime(char[] temp) {
		int k=6;
		String tempWord="";
		while(k<temp.length) {
			tempWord = tempWord + temp[k];
			k++;
		}
		return tempWord;
	}

	/**
	 * This method takes in the Start Date of a task.
	 * If "#t" is detected, it will add a Day to the date.
	 * The start date is then validated before returning.
	 * 
	 * @param temp  #300512 for 30th May 2012 or #300512-160612 for 30th May 2012 to 16th June 2012
	 * @return tempWord  "300512" in String format or Empty String if Start date is invalid.
	 */
	public String CheckDate(char[] temp) {
		int k=1;
		String tempWord="";

		if(temp.length > 1) {
			if(temp.length == 7 || temp.length == 14) {
				for(k=1; k<7; k++)
					tempWord = tempWord + temp[k];
			}
			else if(temp[1]=='t') {
				tempWord = Tomorrow(1);
			}
		}
		if(ValidateDate(tempWord)) {
			return tempWord;
		}		
		else 
			return "";
	}

	/**
	 * This method takes in the Start-Due Date of a task.
	 * The due date is then validated before returning.
	 * 
	 * @param temp  #300512-160612 for 30th May 2012 to 16th June 2012
	 * @return tempWord  "160612" in String format or Empty String if Due date is invalid.
	 */
	public String CheckDate2(char[] temp) { 
		int k=8;
		String tempWord="";

		while(k<temp.length) {
			tempWord = tempWord + temp[k];
			k++;
		}
		
		if(ValidateDate(tempWord)) {
			return tempWord;
		}		
		else 
			return "";
	}

	/**
	 * This method takes in Time in String format and calls for ValidateTime()
	 * to validate that the Time is in correct format and is valid
	 * 
	 * @param time  (String) "1700" for 5pm
	 * @return true  for Valid time format.
	 */
	public boolean CheckTime(String time) {
		TimeDateValidator timedateValidator = new TimeDateValidator();
		if(timedateValidator.ValidateTime(time))
			return true;
		System.out.println("Invalid Time - CheckTime fn");
		return false;
	}
	
	
	/**
	 * This function adds the number of day(s) as indicated in its parameter
	 * to today's date and return it in String format.
	 * 
	 * @param day  Number of days to be added to today's date.
	 * @return  Today's date + number of days added, in String format.
	 */
	public String Tomorrow(int day) {
		String temp;
		SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy");
		Calendar c = Calendar.getInstance();

		c.add(Calendar.DATE, day);  // number of days to add
		temp = sdf.format(c.getTime());  

		return temp;
	}
	
	/**
	 * This method takes in the Date in String format to be validated.
	 * 
	 * @param Date  in String format: ie, "300512"
	 * @return true  if Date given is valid.
	 */
	public boolean ValidateDate(final String Date) {
		SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy");
	    Date testDate = null;
	    System.out.println("Validate Date fn "+Date);
	    
	    try {
	    	testDate = sdf.parse(Date);
	    }
	    catch (ParseException e) {
	    	System.out.println(Date);
	    	System.out.println("the date you provided is in an invalid date format.");
	    	return false;
	    }
	    if (!sdf.format(testDate).equals(Date)) {
	    	System.out.println("the date you provided is invalid.");
	    	return false;
	    }
	    else {
	    	System.out.println("the date you provided is OKAY.");
	    	return true;
	    }
	}
		
	//this part for validating time
	private Pattern pattern;
	private Matcher matcher;
	private static final String TIME24HOURS_PATTERN = "([01]?[0-9]|2[0-3])[0-5][0-9]";
	
	public TimeDateValidator(){
	}
		
	/**
	 * Validate time in 24 hours format with regular expression
	 * 
	 * @param time  time address for validation
	 * @return true  valid time format, false invalid time format
	 */
	public boolean ValidateTime(final String time){
		pattern = Pattern.compile(TIME24HOURS_PATTERN);
		matcher = pattern.matcher(time);
	    return matcher.matches();
	}
}
